// indexedDB.js，浏览器本地数据库操作

export default {
  // indexedDB兼容
  indexedDB: window.indexedDB || window.webkitindexedDB || window.msIndexedDB || mozIndexedDB,
  // 打开数据库
  // 新对象储存空间newStore参数：newStore.name、newStore.key
  // 新增对象存储空间要更改数据库版本
  openDB: function (dbname, version, db, newStore, callback) {
      var version = version || 1;
      var request = this.indexedDB.open(dbname, version);
      request.onerror = function (event) {
          console.log('IndexedDB数据库打开错误');
      };
      request.onsuccess = function (event) {
        db = event.target.result;
        if (callback && (typeof callback === 'function')) {
            callback(db);
        }
      };
      // onupgradeneeded，调用创建新的储存空间
      request.onupgradeneeded = function (event) {
          var db = event.target.result;
          if (newStore) {
              if (!db.objectStoreNames.contains(newStore.name)) {
                var objectStore = db.createObjectStore(newStore.name, {
                  keyPath: newStore.key,
                });
                objectStore.createIndex('historyName', 'historyName', { unique: false });
              }
          }
      };
  },
  // 删除数据库
  deleteDB: function (dbname, callback) {
      var deleteQuest = this.indexedDB.deleteDatabase(dbname);
      deleteQuest.onerror = function () {
          console.log('删除数据库出错');
      };
      deleteQuest.onsuccess = function () {
          if (callback && (typeof callback === 'function')) {
              callback();
          }
      }
  },
  // 关闭数据库
  closeDB: function (dbname) {
      dbname.close();
      console.log('数据库已关闭');
  },
  // 新增数据
  addData: function (db, storename, data, callback) {
    var request = db.transaction(storename, 'readwrite')
      .objectStore(storename)
      .add(data);

    request.onsuccess = function (event) {
      if (callback && (typeof callback === 'function')) {
        callback();
      }
    };

    request.onerror = function (event) {
      console.log('数据写入失败');
    }
  },
  putData:function (db, storename, data, callback) {
    var request = db.transaction(storename, 'readwrite')
      .objectStore(storename)
      .put(data);

    request.onsuccess = function (event) {
      if (callback && (typeof callback === 'function')) {
        callback();
      }
    };

    request.onerror = function (event) {
      console.log('数据数据失败');
    }
  },
  readAll:function(db,storeName){
    var transaction = db.transaction(storeName);
    var dataList = [];
    var objectStore = transaction.objectStore(storeName);
    return new Promise((resolve, reject) => {
      objectStore.openCursor().onsuccess = function (event) {
          var cursor = event.target.result;
          if (cursor) {
              dataList.push(cursor.value)
              cursor.continue();
          } else {
              resolve(dataList);
          }

      };
  })
  },
  // 删除数据
  deleteData: function (db, storename, key, callback) {
    var store = db.transaction(storename, 'readwrite').objectStore(storename);
    store.delete(key);
    if (callback && (typeof callback === 'function')) {
      callback();
    }
  },
  // 清空数据
  clearData: function (db, storename, callback) {
    var store = db.transaction(storename, 'readwrite').objectStore(storename);
    store.clear();
    if (callback && (typeof callback === 'function')) {
      callback();
    }
  },
}
